package cn.itcast.mq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.sql.SQLOutput;

@Component
@Slf4j
public class listener {
//    @RabbitListener(queues = "simple.queue")
//    public void springSimpleQueueMessage(String message){
//        System.out.println("message"+message);
//        System.out.println("收到消息！"+message);
//    }
    @RabbitListener(queues = "simple.queue")
    public void springWorkQueue1(String str) throws InterruptedException {
        System.out.println(str);
        Thread.sleep(100);
    }
    @RabbitListener(queues = "simple.queue")
    public void springWorkQueue2(String str) throws InterruptedException {
        System.out.println(str);
        Thread.sleep(200);
    }

    //监听消息队列fanout.queue1   广播模式  该队列已经在配置内中绑定了
    @RabbitListener(queues = "fanout.queue1")
    public void springFanoutQueue1(String str){
        System.out.println("message is :"+str+"this is queue1");
//        System.out.println("this is queue1");

    }

    //监听消息队列fanout.queue2  广播模式  该队列已经在配置内中绑定了
    @RabbitListener(queues = "fanout.queue2")
    public void springFanoutQueue2(String str){
        System.out.println("message is:"+str+"this is queue2");
//        System.out.println("this is queue2");
    }


    //监听路由队列Direct.queue1
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name="direct.queue11"),
            exchange =@Exchange(name = "itCast.direct1",type = ExchangeTypes.DIRECT),
            key = "red"
    ))
    public void DirectListener1(String str){
        System.out.println("message is :"+str+"this is direct.queue1");
        System.out.println("key is  red");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name="direct.queue22"),
            exchange =@Exchange(name = "itCast.direct1",type = ExchangeTypes.DIRECT),
            key = "blue"
    ))
    public void DirectListener2(String str){
        System.out.println("message is :"+str+"this is direct.queue2");
        System.out.println("key is blue");
    }


    //主题方式监听
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name="topic.queue1"),
            exchange = @Exchange(name="itCast.topic",type=ExchangeTypes.TOPIC),
            key = "#.fei"
    ))
    public void TopicListener1(String str){
        System.out.println("message is:"+str+"this is topic.topic");
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name="topic.queue2"),
            exchange = @Exchange(name="itCast.topic",type=ExchangeTypes.TOPIC),
            key="fei.#"
    ))
    public void TopicListener2(String str){
        System.out.println("message is:"+str+"this is queue2");
    }


}
